#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer();
        timer->setInterval(2000);
        timer->start();
        ui->vSliderVolume->setVisible(false);
            QString fileName = "D:/music";
            QDir dir(fileName);
            QStringList nameFilters;
            nameFilters << "*.mp3";
            QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
            ui->lwMusicList->addItems(files);
            m_playPath = fileName;
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_btnPlay_clicked()
{
    if(QMediaPlayer:: PlayingState == m_mediaPlayer.state()){
            m_mediaPlayer.pause();
            ui->btnPlay->setText("Play");
        }
        else{
            m_mediaPlayer.play();
            ui->btnPlay->setText("Pause");
        }
}


void MainWindow::on_hSliderPlayProgress_sliderMoved(int position)
{
     m_mediaPlayer.setPosition(m_mediaPlayer.duration()*position/100);
}
void MainWindow::setSliderValue()
{
    ui->hSliderPlayProgress->setValue(m_mediaPlayer.position()*100/m_mediaPlayer.duration());
    if(m_mediaPlayer.position()>=m_mediaPlayer.duration()){
            on_btnNextMusic_clicked();
        }
}

void MainWindow::on_vSliderVolume_sliderMoved(int position)
{
    m_mediaPlayer.setVolume(position);
}
void MainWindow::on_lwMusicList_itemDoubleClicked(QListWidgetItem *item)
{
    m_PlayRow = ui->lwMusicList->row(item);
    m_mediaPlayer.stop();
    m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));
    m_mediaPlayer.play();
    ui->hSliderPlayProgress->setValue(m_mediaPlayer.position());
    ui->btnPlay->setText("Pause");
    connect(timer, SIGNAL(timeout()), this, SLOT(setSliderValue()));
    connect(&m_mediaPlayer,SIGNAL(durationChanged(qint64)),this,SLOT(getduration()));
    connect(&m_mediaPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(setPlayTime()));

}

void MainWindow::on_btnAddMusic_clicked()
{
    QString fileName = QFileDialog::getExistingDirectory(NULL,"Select Music Dir",".",NULL);
    QDir dir(fileName);
    QStringList nameFilters;
    nameFilters << "*.mp3";
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    ui->lwMusicList->addItems(files);
    m_playPath = fileName;

}
void MainWindow::on_btnPreMusic_clicked()
{
    if(m_PlayRow == 0){
            m_PlayRow = ui->lwMusicList->count() - 1;
        }
        else{
            m_PlayRow--;
        }
        QListWidgetItem *item = ui->lwMusicList->item(m_PlayRow);
        item->setSelected(true);
        m_mediaPlayer.stop();
        m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));
        m_mediaPlayer.play();
}

void MainWindow::on_btnNextMusic_clicked()
{
    if(m_PlayRow + 1 == ui->lwMusicList->count()){
            m_PlayRow = 0;
        }
        else{
            m_PlayRow++;
        }
        QListWidgetItem *item = ui->lwMusicList->item(m_PlayRow);
        item->setSelected(true);
        m_mediaPlayer.stop();
        m_mediaPlayer.setMedia(QUrl::fromLocalFile(m_playPath+"/"+item->text()));
        m_mediaPlayer.play();
}

void MainWindow::on_btnVolume_clicked()
{
    if(ui->vSliderVolume->isVisible()){
           ui->vSliderVolume->setVisible(false);
       }else{
           ui->vSliderVolume->setVisible(true);
       }
   }

   void MainWindow::on_vSliderVolume_sliderReleased()
   {
        ui->vSliderVolume->setVisible(false);
}
   QString MainWindow::settime(int time)
   {
       int h,m,s;
       time /= 1000;  //获得的时间是以毫秒为单位的
       h = time/3600;
       m = (time-h*3600)/60;
       s = time-h*3600-m*60;
       return QString("%1:%2:%3").arg(h).arg(m).arg(s);
   }

   void MainWindow::getduration()
   {
       playtime = m_mediaPlayer.duration();
       ui->lblMusicTime->setText(settime(playtime));
   }
   void MainWindow::setPlayTime()
   {
       ui->lblPlayTime->setText(settime(m_mediaPlayer.position()));
   }


